<?php

class BusinessAds extends AdBlock {

    public $url;
    public $image;

    /**

     * Returns the static model of the specified AR class.

     * @param string $className active record class name.

     * @return User the static model class

     */
    public static function model($className = __CLASS__) {

        return parent::model($className);
    }

    public function defaultScope() {

        return array(
            'condition' => "t.type = 'business_ads'"
        );
    }

    public function rules() {

        return array_merge(parent::rules(), array(
            array('url', 'length', 'max' => 250),
            array('image', 'length', 'max' => 250),
        ));
    }

    public function beforeSave() {

        if (parent::beforeSave()) {

            $this->value = json_encode(array(
                'url' => $this->url,
                'image' => $this->image
            ));

            $this->type = $this->key = 'business_ads';

            return true;
        }

        return false;
    }

    public function beforeValidate() {

        $this->value = json_encode(array(
            'url' => $this->url,
            'image' => $this->image
        ));

        $this->type = $this->key = 'business_ads';

        parent::beforeValidate();



        return true;
    }

    public function afterFind() {

        parent::afterFind();

        $json = json_decode($this->value);

        $this->url = $json->url;

        $this->image = $json->image;
    }

}